package postgres_datasource

import (
	"fiber_cli/config"
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

var postgresDatabase *gorm.DB

func GetPostgresDB() *gorm.DB {
	return postgresDatabase
}

func init() {
	// dsn data source name
	dsn := fmt.Sprintf("host=%v user=%v password=%v dbname=%v port=%v sslmode=disable", config.Config.Postgres.Host, config.Config.Postgres.Username, config.Config.Postgres.Password, config.Config.Postgres.Database, config.Config.Postgres.Port)

	var err error
	postgresDatabase, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if (err) != nil {
		panic(err)
	}
	fmt.Printf(" %c[1;0;33m%s%c[0m\n", 0x1B, "** pg success init **", 0x1B)
}
